package com.whmnx.my.ui.viewmodel;


import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.base.bean.OrderListBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.ItemViewpagerCaiBinding;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 *
 * @author goldze
 * @date 2018/7/18  财务管理
 */

public class ViewPagerCaiwuItemViewModel extends ItemViewModel<FinancialModel> {
    public int index;

    public int page = 1;
    private int pageSize = 10;
    private FinancialModel viewModel;
    private ItemViewpagerCaiBinding viewDataBinding;
    private Disposable subscribe;

    public void setBinding(ItemViewpagerCaiBinding binding) {
        this.viewDataBinding = binding;

        viewDataBinding.refresh.setOnRefreshListener(refreshLayout -> getSelorder(true));
        viewDataBinding.refresh.setOnLoadMoreListener(refreshLayout -> getSelorder(false));
         autoRefresh2();
    }

    public ViewPagerCaiwuItemViewModel(@NonNull FinancialModel viewModel, int index) {
        super(viewModel);
        this.index = index;
        this.viewModel = viewModel;
        getSelorder(true);
        initView();
    }

    public void autoRefresh2() {
        viewDataBinding.refresh.autoRefresh();

    }


    private void initView() {
        subscribe = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String object){
                        if (object.equals(EventConstants.HUFU)){  //支付回调)
                            getSelorder(true);
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    public void getSelorder(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        if (index == 1){
            params.put("order_Status",1);
        }else if(index == 2){
            params.put("order_Status",2);
        }else if (index == 3){
            params.put("order_Status",3);
        }else if (index == 4){
            params.put("order_Status",4);
        }else if (index == 5){
            params.put("order_Status",5);
        }
        params.put("pageIndex",page);
        params.put("pageSize",pageSize);
        params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.post(viewModel.mFragment.getActivity(), Constants.SELMANAGER, params,new CallBack<List<OrderListBean>>() {
            @Override
            public void onSuccess(List<OrderListBean> orderListBeans) {
               // getOrder();
                if (isRefresh){
                    observableList.clear();
                    for (OrderListBean orderListBean : orderListBeans) {
                        observableList.add(new OrderItemViewModel(viewModel,ViewPagerCaiwuItemViewModel.this,orderListBean));
                    }
                    viewDataBinding.refresh.finishRefresh(true);
                }else {
                    if (orderListBeans.size() > 0) {
                        for (OrderListBean orderListBean : orderListBeans) {
                            observableList.add(new OrderItemViewModel(viewModel,ViewPagerCaiwuItemViewModel.this,orderListBean));
                        }
                        viewDataBinding.refresh.finishLoadMore(true);
                    } else {
                        viewDataBinding.refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }




    //给RecyclerView添加ObservableList
    public ObservableList<OrderItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<OrderItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.grid_order_cai);


    private void getOrder() {
        String order = Constants.ORDER;
        if (!TextUtils.isEmpty(order)) {
            if (order.equals("确认收货")) {
                index = 2;
                getSelorder2();
            }
        }
    }

    private void getSelorder2() {
        Map<String, Object> params = new HashMap<>();
        if (index == 1){
            params.put("order_Status",1);
        }else if(index == 2){
            params.put("order_Status",2);
        }else if (index == 3){
            params.put("order_Status",3);
        }else if (index == 4){
            params.put("order_Status",4);
        }else if (index == 5){
            params.put("order_Status",5);
        }
        params.put("pageIndex",1);
        params.put("pageSize",10);
        params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.post(viewModel.mFragment.getActivity(), Constants.SELMANAGER, params,new CallBack<List<OrderListBean>>() {
            @Override
            public void onSuccess(List<OrderListBean> orderListBeans) {
                observableList.clear();
                for (OrderListBean orderListBean : orderListBeans) {
                    observableList.add(new OrderItemViewModel(viewModel,ViewPagerCaiwuItemViewModel.this,orderListBean));
                }
            }
        });
    }
}
